/**
 * @FileName: ExportBroadbandTextData.java
 * @Author
 * @Description:
 * @Date 2016年4月13日 下午3:22:16
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service.ds;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.kit.Prop;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.service.BroadbandorderService;
import cn.hpclub.server.service.ds.DateUtils.SyncParams;
import cn.hpclub.server.util.InterfaceUtil;

public class ExportBroadbandTextData extends ExportTextDataService{

    private static final String FILE_NAME_PREFIX = "dzgkd_";
    private static final String PATH_DS_YEWU     = "YeWu/";

    public ExportBroadbandTextData(SyncParams params){
        this.params = params;
        Prop prop = DsPropKit.getProp();
        this.ftpParam = new FtpParam(prop.get("dzgkdHost"), prop.getInt("dzgkdServerPort"), prop.get("dzgkdUserName"),
                prop.get("dzgkdPassword"), prop.get("dzgkdEncoding"), prop.get("dzgkdRemotePath"));
    }

    @Override
    String getSql(SyncParams params){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo(params);
        String select = BroadbandorderService.service.getSqlSelect();
        String sqlExceptSelect = BroadbandorderService.service.getSqlExceptSelect(searchInfo);
        return String.format("%s %s", select, sqlExceptSelect);
    }

    @Override
    String getFileName(SyncParams params){
        return String.format("%s%s%s", FILE_NAME_PREFIX, params.tag, FILE_EXTENSION);
    }

    @Override
    String getFilePath(String fileName){
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM");
        String monthDirName = dateFormater.format(new Date());

        String dirPath = Common.getRootDir() + PATH_DS_YEWU + monthDirName;
        Common.checkDir(dirPath);

        return dirPath + "/" + fileName;
    }

    @Override
    String convertRecordToString(Record item){
        String[] columns = { "boss_no", "gender", "telephone", "city", "area", "name", "other_contact_phone",
                "install_area", "member_phone", "expected_install_time", "call_time", "feed_back", "business_code",
                "install_address", "reservation_state", "create_date", "end_date", };
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < columns.length; i++){
            Object object = item.get(columns[i]);
            if("member_phone".equals(columns[i])){
                if(null != object){
                    final String value = object.toString().replace("\n", "").replace("\r", "");
                    String[] phones = value.split(",");
                    for(int j = 0; j < 3; j++){
                        if(j < phones.length){
                            sb.append(phones[j]);
                        }
                        sb.append("$");
                    }
                } else{
                    sb.append("$$$");
                }
            } else{
                String value = object == null ? "" : object.toString().replace("\n", "").replace("\r", "");
                sb.append(value);
                if(i != (columns.length - 1)){
                    sb.append("$");
                }
            }
        }
        return sb.toString();
    }

    @Override
    void saveExportResult(SyncParams params, String filePath){}

    private ConcurrentHashMap<String, String> getSearchInfo(SyncParams params){
        ConcurrentHashMap<String, String> searchMap = new ConcurrentHashMap<String, String>();
        searchMap.put("create_date", String.format("%s|%s", params.start, params.end));
        int chengduId = InterfaceUtil.getCityOrgId("成都");
        if(0 != chengduId){
            searchMap.put("org_id", String.valueOf(chengduId));
        }
        return searchMap;
    }

}
